package com.devshephard.juchm.threadPool;

import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;

/**
 * Future中的submit方法,并行等待
 */
@Slf4j
public class TestSubmit {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService pool = Executors.newFixedThreadPool(2);
        
        Future<String> futureTask1 = pool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                TimeUnit.SECONDS.sleep(3);
                System.out.println("call1");
                return "execute1";
            }
        });

        Future<String> futureTask2 = pool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                TimeUnit.SECONDS.sleep(3);
                System.out.println("call2");
                return "execute2";
            }
        });

        //调用future.get()方法,则会将调用线程阻塞等待,等待线程完成后继续执行
        //当有多个future调用get()方法时,则会并行等待(并非顺序等待)
        System.out.println(futureTask1.get());
        System.out.println(futureTask2.get());
        System.out.println("main");

    }
    
}
